public class Demo {
    public int minOperations(int[] nums, int x){
         int numSum = 0;
         int sum = 0;
         int len = 0;
         int left = 0;
         int right = 0;
         int newlen = 0;
         for(int num : nums){
             numSum += num;
         }
         if (numSum < x) return -1;
         if (numSum == x) return nums.length;
         int target = numSum - x;
         while (right < nums.length){
             while (sum < target && right < nums.length){
                 sum += nums[right];
                 right++;
                 len++;
             }
             while (sum >= target && left < nums.length){
                 if (sum == target) newlen = Math.max(len,newlen);
                 sum -= nums[left];
                 left++;
                 len--;
             }
         }
         if (newlen == 0) return -1;
         return nums.length - newlen;
    }
}
